package leetcode.editor.cn;

import 栈和队列.ArrayQueue;

import java.util.ArrayDeque;
import java.util.LinkedList;

public class P404SumOfLeftLeaves {
    public static void main(String[] args) {
        Solution solution = new P404SumOfLeftLeaves().new Solution();

    }

    //leetcode submit region begin(Prohibit modification and deletion)

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     * int val;
     * TreeNode left;
     * TreeNode right;
     * TreeNode() {}
     * TreeNode(int val) { this.val = val; }
     * TreeNode(int val, TreeNode left, TreeNode right) {
     * this.val = val;
     * this.left = left;
     * this.right = right;
     * }
     * }
     */
    class Solution {
        public int sumOfLeftLeaves(TreeNode root) {
            int sum = 0;
            if (root.left != null) {
                if (root.left.left == null && root.left.right == null) {
                    sum += root.left.val;
                }
                sum += sumOfLeftLeaves(root.left);
            }
            if (root.right != null) {
                sum += sumOfLeftLeaves(root.right);
            }
            return sum;
        }

        /**
         * 广度优先
         * @param root
         * @return
         */
        public int sumOfLeftLeavesLevelOrder(TreeNode root) {
            int sum = 0;
            LinkedList<TreeNode> queue = new LinkedList<>();
            queue.add(root);
            while (!queue.isEmpty()) {
                TreeNode poll = queue.poll();
                if (poll.left != null) {
                    if (poll.left.left == null && poll.left.right == null) {
                        sum += poll.left.val;
                    }
                }
                if (poll.left != null) {
                    queue.offer(poll.left);
                }
                if (poll.right != null) {
                    queue.offer(poll.right);
                }
            }
            return sum;
        }

    }
//leetcode submit region end(Prohibit modification and deletion)

}